﻿Imports System.Data.SqlServerCe

Namespace Data

    Public Class Command
        Private mCommand As SqlCeCommand
        Private mConnection As Connection
        Private mTransaction As TransactionScope

        Friend Sub New(ByVal sql As String, ByVal connection As Connection)
            mConnection = connection
            mCommand = New SqlCeCommand(sql, connection.GetCEConnection)
        End Sub
        Friend Sub New(ByVal sql As String, ByVal transactionScope As TransactionScope)
            mTransaction = transactionScope
            mConnection = transactionScope.Connection
            mCommand = New SqlCeCommand(sql, mConnection.GetCEConnection)
            mCommand.Transaction = mTransaction.CETransaction
        End Sub

        Public Function ExecuteReader() As SqlCeDataReader
            Environment.OnDBTraceInfoCommand(Me)
            Return mCommand.ExecuteReader
        End Function
        Public Function ExecuteNonQuery() As Integer
            Environment.OnDBTraceInfoCommand(Me)
            Dim blnOpened As Boolean = False
            Try
                If mCommand.Connection.State <> ConnectionState.Open Then
                    mCommand.Connection.Open()
                    blnOpened = True
                End If
                Return mCommand.ExecuteNonQuery
            Finally
                If blnOpened Then mCommand.Connection.Close()
            End Try
        End Function

        Public Property Transaction() As TransactionScope
            Get
                Return mTransaction
            End Get
            Set(ByVal value As TransactionScope)
                mTransaction = value
                mCommand.Transaction = value.CETransaction
            End Set
        End Property
        Public ReadOnly Property Connection() As Connection
            Get
                Return mConnection
            End Get
        End Property

        Public Sub AddParameterWithValue(ByVal parameterName As String, ByVal value As Object)
            mCommand.Parameters.AddWithValue(parameterName, value)
        End Sub
        Public Function AddParameter(ByVal parameterName As String, ByVal type As System.Data.SqlDbType) As SqlCeParameter
            Return mCommand.Parameters.Add(parameterName, type)
        End Function
        Public Sub AddParameterRange(ByVal parameters() As SqlCeParameter)
            If parameters Is Nothing Then Return
            mCommand.Parameters.AddRange(parameters)
        End Sub
        Public Function GetParameter(ByVal parameterName As String) As SqlCeParameter
            Return mCommand.Parameters(parameterName)
        End Function

        Public Property CommandText() As String
            Get
                Return mCommand.CommandText
            End Get
            Set(ByVal value As String)
                mCommand.CommandText = value
            End Set
        End Property

    End Class
End Namespace